﻿// ***********************************************************************
// Assembly         : OSS.BIM
// Author           : TianTeng
// Created          : 08-31-2018
//
// Last Modified By : TianTeng
// Last Modified On : 09-03-2018
// ***********************************************************************
// <copyright file="BimManagerApp.cs" company="北京智帆高科科技有限公司">
//     Copyright © 北京智帆高科科技有限公司 2018
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.IO;
using System.Windows.Forms;
using OSS.BIM.Commands.ProjcetCmds;
using OSS.BIM.Commands.TestCmds;
using OSS.BIM.Commands.ToolCmds;
using OSS.BIM.Model;
using OSS.BIM.Properties;
using OSS.BIM.UI.Controls;
using OSS.BIM.Utility;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Docking;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using Osg;
using OSS.BIM.SceneManage;
using Zf3DPlatform.osgEarth.Commands.AnimationCmds;
using Zf3DPlatform.osgEarth.Commands.MeasuringToolsCmds;
using Zf3DPlatform.osgEarth.Commands.ViewCmds;
using ZFGK.Addins;
using ZFGK.Definition;
using ZFGK.DevExprss.Utility;
using ZFGK.IO;
using ZFGK.OSG.UI;
using ZFGK.OSG.UI.Commands;
using ZFGK.osgEarth.PlatformGUI.Commands;
using ZFGK.osgEarth.PlatformGUI.Commands.ImageLayerCmds;
using ZFGK.osgEarth.PlatformGUI.Commands.ViewCmds;
using ZFGK.osgEarth.PlatformGUI.osgEarth.Commands.ToolCmds;
using ZFGK.osgEarth.UI;
using ZFGK.osgEarth.UI.Commands.DigitalEarthCmds;
using ZFGK.Utility;
using ZFGK.WinForms.Forms;
using ZfPlatform.DevExpressGUI;
using ZfWebEarth;

namespace OSS.BIM
{
    /// <summary>
    /// Class BimManagerApp.
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendApp" />
    public class BimManagerApp : IExtendApp
    {
        public static MainRibbonForm mainRibbonForm;

        /// <summary>
        /// Starts up.
        /// </summary>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public bool StartUp(ExtendAppData data)
        {
            if (!BmDatabaseUtil.CheckAndUpdate()) return false;

            BmConfigUtil.LoadAllConfig();

            mainRibbonForm = data.MainForm as MainRibbonForm;

            mainRibbonForm.Icon = Resources.earth_BIM;
            mainRibbonForm.Text = "BIM运管平台";
            InitRibbon(mainRibbonForm.ribbonControl);
            
            #region 添加浮动窗体
            // 添加停靠窗体
            BmTreeListManager.treeList.PopupMenu = mainRibbonForm.popupMenu;
            BmTreeListManager.treeList.Dock = DockStyle.Fill;
            DockPanel dockPanel1 = DockPanelUtil.New(BmTreeListManager.treeList,
                "机场", new Guid("70a62a0b-1c55-4e72-bef0-661a97c3e934"), null);
            dockPanel1.Dock = DockingStyle.Left;

            PropertiesGridControl.Instance.PopupMenu = mainRibbonForm.popupMenu;
            PropertiesGridControl.Instance.Dock = DockStyle.Fill;
            DockPanel dockPanel2 = DockPanelUtil.New(PropertiesGridControl.Instance,
                "属性", new Guid("ED37992D-6DC2-4BD8-8A5C-BBD2C40522A7"), null);
            dockPanel2.Dock = DockingStyle.Right;

            mainRibbonForm.dockManager.RootPanels.AddRange(new[] { dockPanel1, dockPanel2 });
            mainRibbonForm.Controls.AddRange(new[] { dockPanel1, dockPanel2 });
            #endregion

            // 显示测试命令
            mainRibbonForm.ribbonControl.DoubleClick += RibbonControlOnDoubleClick;
            mainRibbonForm.Load += delegate(object sender, EventArgs args)
            {
                BmTreeListManager.InitTreeViewModel();
                // 模型加载之后再使用树控件控制模型显示
                BmTreeListManager.TreeList.AfterCheckNode += BmTreeListManager.BmTreeListOnAfterCheckNode;
                BmTreeListManager.TreeList.NodeCellStyle += BmTreeListManager.BmTreeListOnNodeCellStyle;
                BmTreeListManager.treeList.PreShowPopup += OnPreShowPopup;
                BmTreeListManager.TreeList.AfterFocusNode += TreeListOnAfterFocusNode;
            };

            mainRibbonForm.Closed += (sender, args) => BmConfigUtil.SaveAllConfig();

            return true;
        }

        public static OsgEarthViewForm ActiveEarthViewForm
        {
            get { return mainRibbonForm.ActiveMdiChild as OsgEarthViewForm; }
        }

        private void TreeListOnAfterFocusNode(object sender, NodeEventArgs e)
        {
            var activeEarthViewForm = ActiveEarthViewForm;
            if (activeEarthViewForm == null)
                return;
            var osgEarthObj = activeEarthViewForm.OsgEarthViewCtrl.OsgEarthObj;
            ModelElement modelElement = e.Node.Tag as ModelElement;
            if (modelElement != null)
            {
                osgEarthObj.FxClear();
                osgEarthObj.OutlineNode(modelElement.GetGroupName(), modelElement.ModelElementId.ToString(), new Vec4f(1, 0, 0, 1));
            }
            PropertiesGridControl.Instance.ModelElement = modelElement;
        }

//         private void OsgEarthViewOnPickNodeEvent(object sender, PickNodeEventArgs e)
//         {
//             ModelElement modelElement = ModelElementDbUtil.GetAt(ConvertUtil.ToInt32(e.ModelId));
//             if (modelElement != null && modelElement.ModelGropeId == ConvertUtil.ToInt32(e.GroupName))
//             {
//                 OsgEarthFormInstance.OsgEarthViewCtrl.CancelHighlight();
//                 OsgEarthFormInstance.OsgEarthViewCtrl.OutlineNode(modelElement.GetGroupName(), modelElement.ModelElementId.ToString(), new Vec4f(1, 0, 0, 1));
//                 PropertiesGridControl.Instance.ModelElement = modelElement;
//             }
//         }


        #region 菜单
        /// <summary>
        /// Adds the user menu.
        /// </summary>
        /// <param name="ribbonControl">The ribbon control.</param>
        public static void InitRibbon(RibbonControl ribbonControl)
        {
            // 快捷按钮
            ribbonControl.Toolbar.ItemLinks
                .NewButton("飞往", btn => btn.Tag = (typeof(FlyToCmd)), btn => btn.SetImage(Resources.paper_plane, false)
                    , btn => btn.Hint = ("飞往记录的视点"))
                .NewButton("位置回正",
                    btn => btn.Tag = (typeof(WeiZhiHuiZhengCmd)), btn => btn.SetImage(Resources.compass, false)
                    , btn => btn.Hint = ("以顶视图查看位置"));

            #region 程序菜单
            // 程序
            ribbonControl.Page("程序").Group("显示").ItemLinks
                .NewButton("飞往", btn => btn.Tag = (typeof(FlyToCmd)), btn => btn.SetImage(Resources.paper_plane, true)
                    , btn => btn.Hint = ("飞往记录的视点"))
                .NewButton("位置回正",
                    btn => btn.Tag = (typeof(WeiZhiHuiZhengCmd)), btn => btn.SetImage(Resources.compass, false)
                    , btn => btn.Hint = ("以顶视图查看位置"))
                .NewButton("视图截屏", btn => btn.Tag = (typeof(ViewScreenshotCmd)), btn => btn.SetImage(Resources.截图, false)
                    , btn => btn.Hint = ("截取当前程序的视图"));

            ribbonControl.Page("程序").Group("动画").ItemLinks
                .NewButton("视点窗口", btn => btn.Tag = (typeof(AnimationViewPointShowCmd)), btn => btn.SetImage(Resources.animation_Point, true))
                .NewButton("运行动画", btn => btn.Tag = (typeof(AnimationRunCmd)), btn => btn.SetImage(Resources.animation, true));

            ribbonControl.Page("程序").Group("测量").ItemLinks
                .NewButton("距离",
                    btn => btn.Tag = (typeof(MeasuringDistanceCmd)), btn => btn.SetImage(Resources.MeasuringDistance, true))
                .NewButton("面积", btn => btn.Tag = (typeof(MeasuringAreaCmd)), btn => btn.SetImage(Resources.MeasuringArea, true));

            ribbonControl.Page("程序").Group("图层").ItemLinks
                .NewLinkContainer("卫星影像", btn => btn.SetImage(Resources.satellite, true));
            var barButtonGroup = ribbonControl.Page("程序").Group("图层").ItemLinks.GetBarItem("卫星影像") as BarLinkContainerItem;
            barButtonGroup.ItemLinks
                .NewCheck("GoogleEarth", btn => btn.Tag = (new CmdInfo(typeof(UpdateImageLayerCmd), SatelliteImageType.GOOGLE_EARTH_TILE)), btn => btn.Checked = (true))
                //.NewCheck("GoogleMap", btn => btn.Tag = (new CmdInfo(typeof(UpdateImageLayerCmd), SatelliteImageType.GOOGLE_MAP_TILE)))
                .NewCheck("GoogleMapChina(有偏)", btn => btn.Tag = (new CmdInfo(typeof(UpdateImageLayerCmd), SatelliteImageType.GOOGLE_ChinaSatelliteMap_TILE)))
                .NewCheck("BingChina", btn => btn.Tag = (new CmdInfo(typeof(UpdateImageLayerCmd), SatelliteImageType.BING_SatelliteMap_TILE)))
                .NewCheck("天地图", btn => btn.Tag = (new CmdInfo(typeof(UpdateImageLayerCmd), SatelliteImageType.TIANDITU_MAP_TILE)));

            ribbonControl.Page("程序").Group("图层").ItemLinks
                .NewLinkContainer("添加", btn => btn.SetImage(Resources.layers_map, true));
            var barButtonGroup2 = ribbonControl.Page("程序").Group("图层").ItemLinks.GetBarItem("添加") as BarLinkContainerItem;
            barButtonGroup2
                .NewButton("高程", btn => btn.Tag = (typeof(AddElevationLayerCmd)), btn => btn.SetImage(Resources.maps, false))
                .NewButton("影像", btn => btn.Tag = (typeof(AddImageLayerCmd)), btn => btn.SetImage(Resources.satellite, false));

            ribbonControl.Page("程序").Group("图层").ItemLinks
                .NewLinkContainer("显示隐藏", btn => btn.SetImage(Resources.layers, true));
            var barButtonGroup3 = ribbonControl.Page("程序").Group("图层").ItemLinks.GetBarItem("显示隐藏") as BarLinkContainerItem;
            barButtonGroup3
                .NewButton("路网", btn => btn.Tag = (typeof(HideOrShowLuWangCmd)), btn => btn.SetImage(Resources.road, false))
                .NewButton("县级边界", btn => btn.Tag = (typeof(ShowCountiesBorderCmd)), btn => btn.SetImage(Resources.milestone, false))
                /*.NewButton("等高线", btn => btn.Tag = (typeof(ContourLineShowCmd)).SetImage(Resources.ContourLine, false))*/;

            ribbonControl.Page("程序").Group("查询").ItemLinks
                .NewButton("位置查询", btn => btn.Tag = (typeof(QueryLocationCmd)), btn => btn.SetImage(Resources.loaction, true));

            ribbonControl.Page("程序").Group("配置").ItemLinks
                .NewButton("修改时间", btn => btn.Tag = (typeof(ChangeDateTimeCmd)), btn => btn.SetImage(Resources.calendar_clock, true))
                .NewButton("打开配置路径", btn => btn.Tag = (typeof(OpenAsAppConfigPathCmd)), btn => btn.SetImage(Resources.config, true))
                .NewButton("设置", btn => btn.Tag = (typeof(AsSettingCmd)), btn => btn.SetImage(Resources.config, true));
            #endregion

            ribbonControl.Page("工程").Group("项目").ItemLinks
                .NewButton("新建", btn => btn.Tag = (typeof(ProjectNewCmd)), btn => btn.SetImage(Resources.project, true))
                .NewButton("导入", btn => btn.Tag = (typeof(ProjectImportCmd)), btn => btn.SetImage(Resources.Import, true));

            ribbonControl.Pages.Move(0, ribbonControl.Pages.Count - 1);
        }

        /// <summary>
        /// Ribbons the control on double click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="eventArgs">The <see cref="EventArgs"/> _osgEarthFormInstance containing the event data.</param>
        private void RibbonControlOnDoubleClick(object sender, EventArgs eventArgs)
        {
            //var ribbonControl = OsgEarthViewForm.GetInstance().RibbonControl;
            var ribbonControl = mainRibbonForm.ribbonControl;
            if (!ribbonControl.ContainsPage("功能测试"))
            {
                var computerName = System.Environment.GetEnvironmentVariable("ComputerName");
                if (string.Compare(computerName, "TianTeng-PC", StringComparison.CurrentCultureIgnoreCase) != 0
                    && string.Compare(computerName, "TianTeng-Zhifan", StringComparison.CurrentCultureIgnoreCase) != 0
                    && string.Compare(computerName, "liao-PC", StringComparison.CurrentCultureIgnoreCase) != 0)
                {
                    var inputOneValForm = new InputOneValForm("测试功能密码", RegexType.Custom);
                    if (inputOneValForm.ShowDialog(/*OsgEarthFormInstance*/) != DialogResult.OK
                        || inputOneValForm.InputValue != "1234")
                        return;
                }

                ribbonControl.Page("功能测试").Group("田腾测试").ItemLinks
                    .NewButton("田腾测试", btn => btn.Tag = (typeof(BmTianTengTestCmd)))
                    .NewButton("OsgView", btn => btn.Tag = (typeof(ShowOsgViewFormCmd)));

                ribbonControl.SelectedPage = ribbonControl.Page("功能测试");
            }
        }

        /// <summary>
        /// Called when [show tree list popup menu].
        /// </summary>
        /// <param name="treeListPopupMenu">The tree list popup menu.</param>
        /// <param name="treeNode">The tree node.</param>
        public static void OnPreShowPopup(PopupMenu treeListPopupMenu, TreeListNode treeNode)
        {
            if (treeNode.Tag == null && treeNode.GetValue(0).ToString() == "项目")
            {
                treeListPopupMenu
                    .NewButton("添加", btn => btn.Tag = (new CmdInfo(typeof(ProjectNewCmd), treeNode.Tag)), btn => btn.SetImage(Resources.project, false));

            }
            if (treeNode.Tag is Project)
            {
                // 使用数据库中最新的数据
                var project = treeNode.Tag as Project;

                treeListPopupMenu
                    .NewButton("编辑", btn => btn.Tag = (new CmdInfo(typeof(ProjectEditCmd), project)), btn => btn.SetImage(Resources.project, false))
                    .NewButton("定位", btn => btn.Tag = (new CmdInfo(typeof(ProjectLocationCmd), project)), btn => btn.SetImage(Resources.project, false))
                    .NewButton("更新边界", btn => btn.Tag = (new CmdInfo(typeof(ProjectUpdateBorderPointsCmd), project)), btn => btn.SetImage(Resources.project, false))
                    .NewButton("场地平整", btn => btn.Tag = (new CmdInfo(typeof(ProjectSiteFormationCmd), project)), btn => btn.SetImage(Resources.project, false))
                    .NewButton("导入", true, btn => btn.Tag = (new CmdInfo(typeof(ProjectImportCmd), project)), btn => btn.SetImage(Resources.project, false));
            }
        }

        /// <summary>
        /// Called when [show grid view popup menu].
        /// </summary>
        /// <param name="treeListPopupMenu">The tree list popup menu.</param>
        /// <param name="row">The row.</param>
        public static void OnShowGridViewPopupMenu(PopupMenu treeListPopupMenu, object row)
        {
        }

        #endregion
    }
}
